package com.weiaibenpao.demo.chislim.sport;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.hurui.activity.HBaseActivity;
import com.weiaibenpao.demo.chislim.sport.utils.Constant;
import com.weiaibenpao.demo.chislim.sport.utils.ShareUtils;
import com.weiaibenpao.demo.chislim.util.Default;

import java.lang.ref.WeakReference;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.weiaibenpao.demo.chislim.util.Default.SPORT_HISTORY_URL;


public class SportRecordShareActivity extends HBaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.myShare)
    TextView myShare;
    @BindView(R.id.record_web_view)
    WebView webView;
    private String url;
    private WeakReference<Activity> weakReference=new WeakReference<Activity>(this);
    private Context context;
    private ShareUtils share;
    private String distance;
    private String marathonTitle;
    private boolean isMarathon;
    private boolean isFinished;
    private boolean isShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sport_record);
        ButterKnife.bind(this);
        context = weakReference.get();
        if(getIntent() != null){
            url = getIntent().getStringExtra(SPORT_HISTORY_URL);
            distance = getIntent().getStringExtra(Constant.RUN_DIS);
            marathonTitle = getIntent().getStringExtra(Constant.MARATHON_TITLE);
            isMarathon = getIntent().getBooleanExtra(Constant.IS_MARATHON,false);
            isFinished = getIntent().getBooleanExtra(Constant.IS_MATCH_FINISHED,false);
        }
        initData();
    }

    private void initData() {
        share = new ShareUtils(context);

        //设置相关属性
        /**
         * WebSettings用来设置WebView的相关属性
         */
        WebSettings webSettings = webView.getSettings();
        //设置WebView支持JavaScript
        webSettings.setJavaScriptEnabled(true);
        //设置可以访问文件
        webSettings.setAllowFileAccess(true);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        /**
         * 判断页面加载过程
         * 当网络加载缓慢,我们可以再页面加载出来之前设置显示进度条
         * setWebChromeClient一般处理加载进度,设置标题,处理js对话框
         */
        webView.setWebChromeClient(new WebChromeClient(){
            ProgressDialog dialog;
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);

                if(newProgress == 100){     //页面加载完成
                    if(dialog != null && dialog.isShowing()){
                        dialog.dismiss();
                    }
                }else {                     //页面正在加载中
                    if(dialog == null){
                        dialog = new ProgressDialog(context);
                        dialog.setTitle("正在加载");
                        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                        dialog.setProgress(newProgress);
                        dialog.show();
                    }else{
                        dialog.setProgress(newProgress);
                    }
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                SportRecordShareActivity.this.setTitle(title);           //设置标题
                super.onReceivedTitle(view, title);

            }
        });

        /**
         * WebViewClient帮助webView处理一些页面控制和请求通知
         * 当点击webview控件中的链接时,在这里设置显示在webview中,否则,会在浏览器打开
         */
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view,String url){
                //设置网页在webview控件中打开,false在浏览器中打开
                view.loadUrl(url);
                return true;
            }
        });

        webView.loadUrl(Default.PRODUCT_URL +url);
//        Log.e("SportRecordShare","load url is "+PRODUCT_URL+url);
    }

    public static void openActivity(Context context, String url,String distance) {
        Intent intent = new Intent(context, SportRecordShareActivity.class);
        intent.putExtra(Default.SPORT_HISTORY_URL, url);
        intent.putExtra(Constant.RUN_DIS,distance);
        context.startActivity(intent);
    }

    /**
     *
     * @param context
     * @param url
     * @param marathonTitle
     * @param isMarathon
     * @param isFinished
     */
    public static void openActivity(Context context, String url,String marathonTitle,boolean isMarathon,boolean isFinished) {
        Intent intent = new Intent(context, SportRecordShareActivity.class);
        intent.putExtra(Default.SPORT_HISTORY_URL, url);
        intent.putExtra(Constant.MARATHON_TITLE,marathonTitle);
        intent.putExtra(Constant.IS_MARATHON,isMarathon);
        intent.putExtra(Constant.IS_MATCH_FINISHED,isFinished);
        context.startActivity(intent);
    }

    @OnClick({R.id.back, R.id.myShare})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.myShare:
                shareMyRecord();
                break;
        }
    }

    private void shareMyRecord() {
        if(isMarathon){
            share.shareLink(Default.PRODUCT_URL +url,marathonTitle,isFinished);
        }else {
            share.shareLink(Default.PRODUCT_URL +url,distance);
        }
    }

    /**
     * 这是兼容的 AlertDialog
     */
    private void showDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("运动提示");
        builder.setMessage("你尚未分析今天的跑步故事，是否分享？");
        builder.setNegativeButton("放弃", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
            }
        });
        builder.setPositiveButton("分享", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                shareMyRecord();
                isShare=true;
            }
        });
        builder.show();
    }
//    /**
//     * 重写onKeyDown方法
//     */
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if((keyCode == KeyEvent.KEYCODE_BACK)){
//                if(!isShare){
//                    showDialog();
//                }else
//                    finish();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }
}
